package com.hkts.management.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
public class DateConverUtils {
    private static final long nd = 1000 * 24 * 60 * 60;
    private static final long nh = 1000 * 60 * 60;
    private static final long nm = 1000 * 60;
    public static final String DATETIME_FORMATTER = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMATTER = "yyyy-MM-dd";

    /**
     * 计算两个时间段时间差，精确到秒
     *
     * @param startTime 2019-04-10 17:16:11
     * @param endTime   2019-04-10 17:28:17
     * @return
     */
    public static String computationTime(Date startTime, Date endTime) {
        try {
            log.info("开始时间->{}, 结束时间->{}", startTime, endTime);
            long diff = endTime.getTime() - startTime.getTime();
            long day = diff / nd;
            long hour = diff % nd / nh;
            long min = diff % nd % nh / nm;
            long sec = diff % nd % nh % nm / 1000;
            String str = day + "天" + hour + "小时" + min + "分钟" + sec + "秒";
            return str;

        } catch (Exception e) {
            log.info("计算两个时间段时间差出错了, {}", e);
            return null;

        }
    }

    public static String handleDate(String time) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(time);
        Date old = sdf.parse(sdf.format(date));
        Date now = sdf.parse(sdf.format(new Date()));
        long oldTime = old.getTime();
        long nowTime = now.getTime();

        long day = (nowTime - oldTime) / (24 * 60 * 60 * 1000);

        if (day < 1) {  //今天
            return "今天" + hours(date);
        } else if (day == 1) {     //昨天
            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            return "昨天" + hours(date);
        } else {    //可依次类推
            SimpleDateFormat format = new SimpleDateFormat("dd日 HH点");
            return format.format(date);
        }
    }

    public static String hours(Date time) {
        SimpleDateFormat df = new SimpleDateFormat("HH");
        String str = df.format(time);
        int a = Integer.parseInt(str);
        if (a >= 0 && a <= 6) {
            return "凌晨";
        } else if (a > 6 && a <= 12) {
            System.out.println("上午");
            return "上午";
        } else if (a > 12 && a <= 13) {
            return "中午";
        } else if (a > 13 && a <= 18) {
            return "下午";
        } else {
            return "晚上";
        }
    }

    public static String morning_or_afternoon(LocalDateTime now) {
        //        LocalDateTime now = LocalDateTime.now();
        switch (now.get(ChronoField.AMPM_OF_DAY)) {
            case 0:
                if (now.getHour() > 6) {
                    return "上午";
                } else {
                    return "凌晨";
                }
            case 1:
                if (now.getHour() > 18) {
                    return "晚上";
                } else {
                    return "下午";
                }
        }
        return "";
    }

    /**
     * 将某时间字符串转为自定义时间格式的LocalDateTime
     *
     * @param time
     * @param format
     * @return
     */
    public static LocalDateTime parseStringToDateTime(String time, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(time, df);
    }

    public static LocalDateTime parseStringToDateTime(String time) {
        return parseStringToDateTime(time, DATETIME_FORMATTER);
    }
    public static LocalDate parseStringToDate(String time, String format) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
        return LocalDate.parse(time, df);
    }

    public static LocalDate parseStringToDate(String time) {
        return parseStringToDate(time, DATE_FORMATTER);
    }

    /*public static void main(String[] args) throws ParseException {

        String s = handleDate("2021-06-07 19:25:53");
        System.out.println(s);
    }*/

    public static void main(String[] args) {
        LocalDateTime tomorrow = LocalDateTime.now();

        LocalDateTime localDateTime = parseStringToDateTime("2021-08-10 12:00:00");
        // before 5 hours and 30 minutes
        LocalDateTime dateTime = LocalDateTime.now().plus(1, ChronoUnit.DAYS);
        System.out.println(localDateTime);
        System.out.println("additionSubtraction: tomorrow: " + tomorrow);
        System.out.println("additionSubtraction: dateTime: " + dateTime);

    }

    public static List<Date> getTimeSegment(int year, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, month - 1, day, 1, 12, 0);
        cal.set(Calendar.MILLISECOND, 0);
        long startTime = cal.getTimeInMillis();
        cal.set(year, month, 10, 17, 0, 0);
        long endTime = cal.getTimeInMillis();
        final int seg = 5 * 60 * 1000;//五分钟
        ArrayList<Date> result = new ArrayList<Date>((int) ((endTime - startTime) / seg + 1));
        for (long time = startTime; time <= endTime; time += seg) {
            result.add(new Date(time));
        }
        return result;
    }
    /**
     * 日期距今天多少天
     *
     * @param localDateTime 起日期
     */
    public static int intervalDays(LocalDateTime localDateTime){
        LocalDate localDate=LocalDate.now();
        return localDate.compareTo(localDateTime.toLocalDate());
    }
    /**
     * 日期相隔天数
     *
     * @param startLocalDate 起日期
     * @param endLocalDate   止日期
     */
    public static long intervalDays(LocalDate startLocalDate, LocalDate endLocalDate) {
        return endLocalDate.toEpochDay() - startLocalDate.toEpochDay();
    }
    /**
     * 日期时间格式化字符串
     *
     * @param localDateTime 日期时间
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String formatLocalDateTime(LocalDateTime localDateTime) {
        return formatLocalDateTime(localDateTime, DATETIME_FORMATTER);
    }
    /**
     * 日期时间格式化字符串
     *
     * @param localDateTime 日期时间
     * @param pattern       格式化
     */
    public static String formatLocalDateTime(LocalDateTime localDateTime, String pattern) {
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }
}
